#define _ADS1220_C_
/* Includes ---------------------------------------------------------*/
/* USER CODE BEGIN Includes */
//#include "fm33lg0xx.h"
#include "ADS1220.h"
/* USER CODE END Includes */

/* Types ------------------------------------------------------------*/
/* USER CODE BEGIN Types */

/* USER CODE END Types */

/* Constants --------------------------------------------------------*/
/* USER CODE BEGIN Constants */

/* USER CODE END Constants */

/* Macro ------------------------------------------------------------*/
/* USER CODE BEGIN Macro */
#define ADS1220_RESET_CMD            					       (0x06)
#define ADS1220_START_CMD             					       (0x08)

// Configuration Register 0 Settings  00h
#define ADS1220_MUX_SHIFT         						       (4)
#define ADS1220_MUX_AIN0_AIN1     						       (0x00 << ADS1220_MUX_SHIFT)
#define ADS1220_MUX_AIN0_AIN2     						       (0x01 << ADS1220_MUX_SHIFT)
#define ADS1220_MUX_AIN0_AIN3     						       (0x02 << ADS1220_MUX_SHIFT)
#define ADS1220_MUX_AIN1_AIN2     						       (0x03 << ADS1220_MUX_SHIFT)
#define ADS1220_MUX_AIN1_AIN3     						       (0x04 << ADS1220_MUX_SHIFT)
#define ADS1220_MUX_AIN2_AIN3     						       (0x05 << ADS1220_MUX_SHIFT)
#define ADS1220_MUX_AIN1_AIN0     						       (0x06 << ADS1220_MUX_SHIFT)
#define ADS1220_MUX_AIN3_AIN2     						       (0x07 << ADS1220_MUX_SHIFT)
#define ADS1220_MUX_AIN0_AVSS     						       (0x08 << ADS1220_MUX_SHIFT)
#define ADS1220_MUX_AIN1_AVSS     						       (0x09 << ADS1220_MUX_SHIFT)
#define ADS1220_MUX_AIN2_AVSS     						       (0x0a << ADS1220_MUX_SHIFT)
#define ADS1220_MUX_AIN3_AVSS     						       (0x0b << ADS1220_MUX_SHIFT)
#define ADS1220_MUX_REFP_REFN     						       (0x0c << ADS1220_MUX_SHIFT)
#define ADS1220_MUX_AVDD_AVSS     						       (0x0d << ADS1220_MUX_SHIFT)
#define ADS1220_MUX_SHORTED       						       (0x0e << ADS1220_MUX_SHIFT)

#define ADS1220_GAIN_SHIFT        						       (1)
#define ADS1220_GAIN_1            						       (0x00 << ADS1220_GAIN_SHIFT)
#define ADS1220_GAIN_2            						       (0x01 << ADS1220_GAIN_SHIFT)
#define ADS1220_GAIN_4            						       (0x02 << ADS1220_GAIN_SHIFT)
#define ADS1220_GAIN_8            						       (0x03 << ADS1220_GAIN_SHIFT)
#define ADS1220_GAIN_16           						       (0x04 << ADS1220_GAIN_SHIFT)
#define ADS1220_GAIN_32          			 			       (0x05 << ADS1220_GAIN_SHIFT)
#define ADS1220_GAIN_64           						       (0x06 << ADS1220_GAIN_SHIFT)
#define ADS1220_GAIN_128          						       (0x07 << ADS1220_GAIN_SHIFT)

#define ADS1220_PGA_BYPASS        						       (1)
#define ADS1220_USE_PGA           						       (0)

// Configuration Register 1 Settings   01h
#define ADS1220_DATA_RATE_SHIFT   						       (5)
#define ADS1220_DATA_RATE_20SPS   						       (0 << ADS1220_DATA_RATE_SHIFT)
#define ADS1220_DATA_RATE_45SPS   						       (1 << ADS1220_DATA_RATE_SHIFT)
#define ADS1220_DATA_RATE_90SPS   						       (2 << ADS1220_DATA_RATE_SHIFT)
#define ADS1220_DATA_RATE_175SPS  						       (3 << ADS1220_DATA_RATE_SHIFT)
#define ADS1220_DATA_RATE_330SPS  						       (4 << ADS1220_DATA_RATE_SHIFT)
#define ADS1220_DATA_RATE_600SPS  						       (5 << ADS1220_DATA_RATE_SHIFT)
#define ADS1220_DATA_RATE_1000SPS 						       (6 << ADS1220_DATA_RATE_SHIFT)

#define ADS1220_OP_MODE_SHIFT     						       (3)
#define ADS1220_OP_MODE_NORMAL    						       (0 << ADS1220_OP_MODE_SHIFT)
#define ADS1220_OP_MODE_DUTY      						       (1 << ADS1220_OP_MODE_SHIFT)
#define ADS1220_OP_MODE_TURBO     						       (2 << ADS1220_OP_MODE_SHIFT)

#define ADS1220_CONVERSION_SINGLE_SHOT   				       (0x00)
#define ADS1220_CONVERSION_CONTINUOUS    				       (0x04)

#define ADS1220_TEMP_SENSOR_ON           				       (0x02)
#define ADS1220_TEMP_SENSOR_OFF          				       (0x00)

#define ADS1220_BURN_OUT_CURRENT_ON      				       (0x01)
#define ADS1220_BURN_OUT_CURRENT_OFF     				       (0x00)

// Configuration Register 2 Settings   02h
#define ADS1220_VREF_SHIFT               				       (6)
#define ADS1220_VREF_INTERNAL            				       (0 << ADS1220_VREF_SHIFT)
#define ADS1220_VREF_EXT_REF0_PINS       				       (1 << ADS1220_VREF_SHIFT)
#define ADS1220_VREF_EXT_REF1_PINS       				       (2 << ADS1220_VREF_SHIFT)
#define ADS1220_VREF_AVDD                				       (3 << ADS1220_VREF_SHIFT)

#define ADS1220_FIR_FILTER_SHIFT         				       (4)
#define ADS1220_FIR_NONE                 				       (0 << ADS1220_FIR_FILTER_SHIFT)
#define ADS1220_FIR_50_60                				       (1 << ADS1220_FIR_FILTER_SHIFT)
#define ADS1220_FIR_50                   				       (2 << ADS1220_FIR_FILTER_SHIFT)
#define ADS1220_FIR_60                   				       (3 << ADS1220_FIR_FILTER_SHIFT)

#define ADS1220_LOW_SIDE_POWER_OPEN      				       (0x00)
#define ADS1220_LOW_SIDE_POWER_CLOSES    				       (0x04)

#define ADS1220_IDAC_CURRENT_SHIFT       				       (0)
#define ADS1220_IDAC_CURRENT_OFF         				       (0 << ADS1220_IDAC_CURRENT_SHIFT)
#define ADS1220_IDAC_CURRENT_10_UA       				       (1 << ADS1220_IDAC_CURRENT_SHIFT)
#define ADS1220_IDAC_CURRENT_50_UA       				       (2 << ADS1220_IDAC_CURRENT_SHIFT)
#define ADS1220_IDAC_CURRENT_100_UA      				       (3 << ADS1220_IDAC_CURRENT_SHIFT)
#define ADS1220_IDAC_CURRENT_250_UA      				       (4 << ADS1220_IDAC_CURRENT_SHIFT)
#define ADS1220_IDAC_CURRENT_500_UA      				       (5 << ADS1220_IDAC_CURRENT_SHIFT)
#define ADS1220_IDAC_CURRENT_1000_UA     				       (6 << ADS1220_IDAC_CURRENT_SHIFT)
#define ADS1220_IDAC_CURRENT_1500_UA     				       (7 << ADS1220_IDAC_CURRENT_SHIFT)

// Configuration Register 3 Settings   03h
#define ADS1220_IDAC1_MUX_SHIFT          				       (5)
#define ADS1220_IDAC1_DISABLED           				       (0 << ADS1220_IDAC1_MUX_SHIFT)
#define ADS1220_IDAC1_AIN0               				       (1 << ADS1220_IDAC1_MUX_SHIFT)
#define ADS1220_IDAC1_AIN1               				       (2 << ADS1220_IDAC1_MUX_SHIFT)
#define ADS1220_IDAC1_AIN2               				       (3 << ADS1220_IDAC1_MUX_SHIFT)
#define ADS1220_IDAC1_AIN3               				       (4 << ADS1220_IDAC1_MUX_SHIFT)
#define ADS1220_IDAC1_REFP0              				       (5 << ADS1220_IDAC1_MUX_SHIFT)
#define ADS1220_IDAC1_REFN0              				       (6 << ADS1220_IDAC1_MUX_SHIFT)

#define ADS1220_IDAC2_MUX_SHIFT          				       (2)
#define ADS1220_IDAC2_DISABLED           				       (0 << ADS1220_IDAC2_MUX_SHIFT)
#define ADS1220_IDAC2_AIN0               				       (1 << ADS1220_IDAC2_MUX_SHIFT)
#define ADS1220_IDAC2_AIN1               				       (2 << ADS1220_IDAC2_MUX_SHIFT)
#define ADS1220_IDAC2_AIN2               				       (3 << ADS1220_IDAC2_MUX_SHIFT)
#define ADS1220_IDAC2_AIN3               				       (4 << ADS1220_IDAC2_MUX_SHIFT)
#define ADS1220_IDAC2_REFP0              				       (5 << ADS1220_IDAC2_MUX_SHIFT)
#define ADS1220_IDAC2_REFN0              				       (6 << ADS1220_IDAC2_MUX_SHIFT)

#define ADS1220_DRDY_ON_DOUT_DRDY        				       (0x02)
#define ADS1220_DRDY_ON_DRDY_ONLY        				       (0x00)

#define ADS_FILTER_TIMES_AVERAGE                               (20)
#define ADS_FILTER_TIMES_MEDIAN                                (11) // ODD
/* USER CODE END Macro */

/* Variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */

/* Function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FP */

/* USER CODE END FP */

uint32_t ulADS1220GetADCDataMedian(void)
{
	uint32_t ucAryADData[ADS_FILTER_TIMES_MEDIAN] = {0};
	uint8_t ucAryData[3] = {0};
	uint8_t ucFilterCnt = 0;
	uint32_t ucTempData = 0;
	float fADSum = 0;
	uint8_t ucIdx = 0;
	
	vADS1220Config(ADS1220_MUX_AIN0_AIN1 | ADS1220_GAIN_128 | ADS1220_USE_PGA,
	   			   ADS1220_DATA_RATE_90SPS | ADS1220_OP_MODE_NORMAL | ADS1220_CONVERSION_SINGLE_SHOT | ADS1220_TEMP_SENSOR_OFF,
	  			   ADS1220_VREF_EXT_REF0_PINS | ADS1220_FIR_NONE | ADS1220_LOW_SIDE_POWER_OPEN | ADS1220_IDAC_CURRENT_250_UA,
	  			   ADS1220_IDAC1_AIN3 | ADS1220_IDAC2_DISABLED | ADS1220_DRDY_ON_DOUT_DRDY);
	vDelay(10);
	
	for(ucFilterCnt = 0; ucFilterCnt < ADS_FILTER_TIMES_MEDIAN; ucFilterCnt++)
	{
		vADS1220Start();
		while(SPI_MISO());
		vADS1220GetConvData(ucAryData);
		
		ucAryADData[ucFilterCnt] = (((uint32_t)(ucAryData[0])) << 16) | (((uint32_t)(ucAryData[1])) << 8) | ucAryData[2];
		vDelay(10);
	}
	
	for(uint8_t i = 0; i < ADS_FILTER_TIMES_MEDIAN - 1; i++)
	{
		for(uint8_t j = 0; j < ADS_FILTER_TIMES_MEDIAN - 1 - i; j++)
		{
			if(ucAryADData[j] > ucAryADData[j + 1])
			{
				ucTempData = ucAryADData[j];
				ucAryADData[j] = ucAryADData[j + 1];
				ucAryADData[j + 1] = ucTempData;
			}
		}
	}
	
//	vADS1220Config(ADS1220_MUX_AIN0_AIN1 | ADS1220_GAIN_1 | ADS1220_USE_PGA,
//  			       ADS1220_DATA_RATE_90SPS | ADS1220_OP_MODE_NORMAL | ADS1220_CONVERSION_SINGLE_SHOT | ADS1220_TEMP_SENSOR_OFF,
// 				   ADS1220_VREF_EXT_REF0_PINS | ADS1220_FIR_NONE | ADS1220_LOW_SIDE_POWER_OPEN | ADS1220_IDAC_CURRENT_OFF,
// 				   ADS1220_IDAC1_DISABLED | ADS1220_IDAC2_DISABLED | ADS1220_DRDY_ON_DOUT_DRDY);
	ucIdx = (ADS_FILTER_TIMES_MEDIAN - 1) / 2;
	fADSum = ((float)(ucAryADData[ucIdx - 1]) + (float)(ucAryADData[ucIdx]) + (float)(ucAryADData[ucIdx + 1])) / 3.0f;
	
	return (uint32_t)fADSum;
}

uint32_t ulADS1220GetADCDataAverage(void)
{
	double dADCSum = 0;
	uint32_t ulADCVal = 0;
	uint8_t ucAryData[3] = {0};
	
	vADS1220Config(ADS1220_MUX_AIN0_AIN1 | ADS1220_GAIN_128 | ADS1220_USE_PGA,
	   			   ADS1220_DATA_RATE_20SPS | ADS1220_OP_MODE_NORMAL | ADS1220_CONVERSION_SINGLE_SHOT | ADS1220_TEMP_SENSOR_OFF,
	  			   ADS1220_VREF_EXT_REF0_PINS | ADS1220_FIR_NONE | ADS1220_LOW_SIDE_POWER_OPEN | ADS1220_IDAC_CURRENT_250_UA,
	  			   ADS1220_IDAC1_AIN3 | ADS1220_IDAC2_DISABLED | ADS1220_DRDY_ON_DOUT_DRDY);
	vDelay(10);
	
	//FL_LPTIM32_Disable(LPTIM32);
	for(uint8_t ucFilterCnt = 0; ucFilterCnt < ADS_FILTER_TIMES_AVERAGE; ucFilterCnt++)
	{
		vADS1220Start();
		while(SPI_MISO());
		vADS1220GetConvData(ucAryData);
		
		ulADCVal = (((uint32_t)(ucAryData[0])) << 16) | (((uint32_t)(ucAryData[1])) << 8) | ucAryData[2];
		dADCSum += (double)ulADCVal;
		vDelay(10);
	}
	//FL_LPTIM32_Enable(LPTIM32);
	
	ulADCVal = (uint32_t)(dADCSum / ADS_FILTER_TIMES_AVERAGE);
	
//	vADS1220Config(ADS1220_MUX_AIN0_AIN1 | ADS1220_GAIN_1 | ADS1220_USE_PGA,
//  			       ADS1220_DATA_RATE_90SPS | ADS1220_OP_MODE_NORMAL | ADS1220_CONVERSION_SINGLE_SHOT | ADS1220_TEMP_SENSOR_OFF,
// 				   ADS1220_VREF_EXT_REF0_PINS | ADS1220_FIR_NONE | ADS1220_LOW_SIDE_POWER_OPEN | ADS1220_IDAC_CURRENT_OFF,
// 				   ADS1220_IDAC1_DISABLED | ADS1220_IDAC2_DISABLED | ADS1220_DRDY_ON_DOUT_DRDY);
	
	return ulADCVal;
}

void vADS1220Start(void)
{
	uint8_t ucData = 0;
	
	ucData = ucSPIWriteByte(ADS1220_START_CMD);
}

void vADS1220Reset(void)
{
	uint8_t ucData = 0;
	
	ucData = ucSPIWriteByte(ADS1220_RESET_CMD);
}

void vADS1220Config(uint8_t ucRegs00h, uint8_t ucRegs01h, uint8_t ucRegs02h, uint8_t ucRegs03h)
{
	uint8_t ucAryConfig[4];

	ucAryConfig[0] = ucRegs00h;
	ucAryConfig[1] = ucRegs01h;
	ucAryConfig[2] = ucRegs02h;
	ucAryConfig[3] = ucRegs03h;
	
	ucSPIWriteByte(0x43);
	ucSPIWriteByte(ucAryConfig[0]);
	ucSPIWriteByte(ucAryConfig[1]);
	ucSPIWriteByte(ucAryConfig[2]);
	ucSPIWriteByte(ucAryConfig[3]);
}

void vADS1220GetConvData(uint8_t *pucData)
{
	pucData[0] = ucSPIWriteByte(0xff);
	pucData[1] = ucSPIWriteByte(0xff);
	pucData[2] = ucSPIWriteByte(0xff);
}

void vADS1220ReadRegs(uint8_t *pucData)
{
	uint8_t ucData = 0;
	
	ucData = ucSPIWriteByte(0x23);
	pucData[0] = ucSPIWriteByte(0xff);
	pucData[1] = ucSPIWriteByte(0xff);
	pucData[2] = ucSPIWriteByte(0xff);
	pucData[3] = ucSPIWriteByte(0xff);
}
